﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DecoratorPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "admin";
            string password = "123456";

            User user = new User();
            user.Login(name, password);

            if (user.LoginStatus)
            {
                DecoratorForLog log = new DecoratorForLog();
                log.SetComponent(user);

                DecoratorForWeChat wechat = new DecoratorForWeChat();
                wechat.SetComponent(log);

                DecoratorForSMS sms = new DecoratorForSMS();
                sms.SetComponent(wechat);

                sms.LoginSuccess();
            }
            Console.ReadKey();
        }

        class User : Component
        {
            private bool _loginStatus = false;
            public bool LoginStatus { get { return _loginStatus; } }
            public void Login(string username, string password)
            {
                //登录验证
                this._loginStatus = true;
            }

            public override void LoginSuccess()
            {
                Console.WriteLine("登录成功！");
            }
        }

        abstract class Component
        {
            public abstract void LoginSuccess();
        }
        abstract class Decorator : Component
        {
            private Component _component;
            public void SetComponent(Component component)
            {
                _component = component;
            }
            public override void LoginSuccess()
            {
                if (this._component != null)
                {
                    this._component.LoginSuccess();
                }
            }
        }

        class DecoratorForLog : Decorator
        {
            public override void LoginSuccess()
            {
               // base.LoginSuccess();

                Console.WriteLine("记录登录日志");
            }
        }

        class DecoratorForSMS : Decorator
        {
            //public override void LoginSuccess()
            //{
            //    base.LoginSuccess();

            //    Console.WriteLine("发送短信通知");
            //}
        }

        class DecoratorForWeChat : Decorator
        {
            public override void LoginSuccess()
            {
               // base.LoginSuccess();

                Console.WriteLine("发送微信通知");
            }
        }
    }
}
